package zmee.pmas.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author cb
 * @DateTime 2022/12/4 15:55
 * @Description 开标准备审核插件
 */
public class BidOpenAuditPlugin extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("billno");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();
        if ("audit".equals(operationKey)) {
            DynamicObject[] dataEntities = e.getDataEntities();
            for (DynamicObject dataEntity : dataEntities) {
                Object pkValue = dataEntity.getPkValue();
                QFilter qFilter = new QFilter("parentid", "=", pkValue);
                DynamicObject loadSingle = BusinessDataServiceHelper.loadSingle("src_bidopen_config",
                        "id,entryentity,entryentity.scorer", new QFilter[]{qFilter});
                if (null != loadSingle) {
                    List<DynamicObject> ids = new ArrayList<>();
                    DynamicObjectCollection entryentity = loadSingle.getDynamicObjectCollection("entryentity");
                    if (CollectionUtils.isNotEmpty(entryentity)) {
                        DynamicObject dynamicObject = entryentity.get(0);
                        DynamicObjectCollection scorer = dynamicObject.getDynamicObjectCollection("scorer");
                        for (DynamicObject user : scorer) {
                            DynamicObject fbasedataid = user.getDynamicObject("fbasedataid");
                            System.out.println("fbasedataid.getPkValue() = " + fbasedataid.getPkValue());
                            ids.add(fbasedataid);
                        }
                        String billno = dataEntity.getString("billno");
                        DynamicObject aptitudeaudit2 = BusinessDataServiceHelper.loadSingle("src_aptitudeaudit2",
                                "id", new QFilter[]{new QFilter("billno", "=", billno)});
                        if (null != aptitudeaudit2) {
                            QFilter qFilter1 = new QFilter("parentid", "=", pkValue);
                            DynamicObject srcAptitudeconfig2 = BusinessDataServiceHelper.loadSingle("src_aptitudeconfig2",
                                    "id,entryentity,entryentity.scorer,entryentity.note", new QFilter[]{qFilter1});
                            if (null != srcAptitudeconfig2) {
                                DynamicObjectCollection entrys = srcAptitudeconfig2.getDynamicObjectCollection("entryentity");
                                for (DynamicObject entry : entrys) {
                                    DynamicObjectCollection scorers = entry.getDynamicObjectCollection("scorer");
                                    scorers.clear();
                                    for (DynamicObject id : ids) {
                                        DynamicObject dy = new DynamicObject(scorers.getDynamicObjectType());
                                        dy.set("fbasedataId", id);
                                        scorers.add(dy);
                                    }
                                    entry.set("scorer", scorers);
                                }
                                SaveServiceHelper.save(new DynamicObject[]{srcAptitudeconfig2});
                            }
                        }
                    }
                }
            }
        }
    }
}